package com.maoning.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.maoning.product.entity.BrandEntity;
import com.maoning.product.entity.CategoryEntity;
import com.maoning.product.service.BrandService;
import com.maoning.product.service.CategoryService;
import com.maoning.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.maoning.product.entity.CategoryBrandRelationEntity;
import com.maoning.product.service.CategoryBrandRelationService;
import com.maoning.common.utils.PageUtils;
import com.maoning.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author ss
 * @email 361736093@qq.com
 * @date 2021-08-19 10:28:54
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 获取当前品牌关联的所有分类列表
     */
    @GetMapping("/catelog/list")
    public R catelogList(@RequestParam("brandId") Long brandId){
        final List<CategoryBrandRelationEntity> list = categoryBrandRelationService.list(new QueryWrapper<CategoryBrandRelationEntity>()
                .eq("brand_id", brandId));
        return R.ok().put("data", list);
    }

    /**
     * 获取当前分类关联的所有品牌列表
     * 1.controller:处理请求,接收和校验数据
     * 2.service:接收controller传来的数据,进行业务处理
     * 3.controller接收service处理完的数据,封装页面指定的vo
     */
    @GetMapping("/brands/list")
    public R brandsList(@RequestParam(value = "catId",required = true) Long catId){
        final List<BrandEntity> list = categoryBrandRelationService.getBrandsBycatId(catId);
        final List<BrandVo> collect = list.stream().map(item -> {
            final BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(item.getBrandId());
            brandVo.setBrandName(item.getName());
            return brandVo;
        }).collect(Collectors.toList());
        return R.ok().put("data", collect);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 在原有的保存方法上需要做一下增强
     * 这是因为原本的关系表虽然可以满足需要,但是如果数据比较多做笛卡尔积,数据量太大,所以牺牲空间避免做name的关联查询
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        final Long brandId = categoryBrandRelation.getBrandId();
        final BrandEntity brandEntity = brandService.getById(brandId);
        final String brandEntityName = brandEntity.getName();
        final Long catelogId = categoryBrandRelation.getCatelogId();
        final CategoryEntity categoryEntity = categoryService.getById(catelogId);
        final String categoryEntityName = categoryEntity.getName();
        categoryBrandRelation.setBrandName(brandEntityName);
        categoryBrandRelation.setCatelogName(categoryEntityName);
        categoryBrandRelationService.save(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
